<template>
  <cusModal v-model="valueModel" :modalProps="modalProps" class="attachmentTypeModal" :appendToBody="true" :modal="true">
    <el-form :model="ruleForm" ref="ruleForm" label-width="120px" :rules="rules" class="demo-ruleForm">

      <el-form-item label="类型">
        <el-radio-group v-model="ruleForm.typeCode">
          <el-radio label="目录"></el-radio>
          <el-radio label="菜单"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单名称" prop="typeName">
        <el-input v-model="ruleForm.typeName"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单" prop="code">
        <el-input v-model="ruleForm.code" disabled></el-input>
      </el-form-item>
      <el-form-item label="菜单URL" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="授权标识" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
    </el-form>
  </cusModal>
</template>

<script>
import cusModal from "@/components/cus-modal/index.vue";
import { attachmentTypeTableData } from "@/plugins/tableData";
const YesNoConst = [
  {
    label: '是',
    value: '1',
  },
  {
    label: '否',
    value: '0',
  },
];

export const IsHaveConst = [
  {
    label: '有',
    value: '1',
  },
  {
    label: '无',
    value: '0',
  },
];
export default {
  components: { cusModal },
  props: {
    value: {
      required: true,
      type: Boolean,
    },
    id: {
      type: String,
    },
  },
  data() {
    return {
      YesNoConst,
      ruleForm: {},
      rules: {
        typeCode: { required: true, message: '请输入附件类别编码', trigger: ['blur', 'input'] },
        typeName: { required: true, message: '请输入附件类别名称', trigger: ['blur', 'input'] },
        code: { required: true, message: '请输入附件编码', trigger: ['blur', 'input'] },
        name: { required: true, message: '请输入附件名称', trigger: ['blur', 'input'] },
        isOpen: { required: true, message: '请选择是否开启', trigger: ['blur', 'change'] },
        sizeLimit: [
          { required: true, message: '请输入附件类别编码', trigger: ['blur', 'input'] },
          { type: 'number', message: '大小必须为数字值' }
        ]
      }
    }
  },
  methods: {
    onNegativeClick() {
      this.$emit("input", false);
      this.$refs["ruleForm"].resetFields();
      this.ruleForm = {};
    }
  },
  computed: {

    valueModel() {
      return this.value
    },
    modalProps() {
      return {
        title: (this.id ? '编辑' : '新增') + '菜单',
        showClose: false,
        onNegativeClick: (setConfirmLoading) => {
          this.onNegativeClick(false);
          setConfirmLoading(false);
        },
        onPositiveClick: (setConfirmLoading) => {
          this.$refs['ruleForm'].validate((valid) => {
            if (valid) {
              setConfirmLoading(true)

              setTimeout(() => {
                if (this.id) {
                  attachmentTypeTableData.forEach(val => {
                    if (val.id == this.id) {
                      Object.assign(val, this.ruleForm)
                    }
                  })
                } else {
                  attachmentTypeTableData.push(this.ruleForm);
                }
                this.$emit("submitAfter", true);
                this.onNegativeClick();
                setConfirmLoading(false);
              }, 500)

            } else {
              this.$emit("submitAfter", false);
              return false;
            }
          })

        },
      }
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val && this.id) {
          this.ruleForm = JSON.parse(JSON.stringify(attachmentTypeTableData.filter(val => val.id == this.id)[0]));
        }
      }
    }

  }
}
</script>

<style lang="scss">
.attachmentTypeModal {
  .el-select {
    display: block;
  }
}
</style>
